/*
Author : Nguyen Van Ty
Email  : ngvanty03@gmail.com
Date   : 12/12/2010 

*/

using System;
using System.Collections.Generic;
using System.Text;
using log4net;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Utility;
using BusinessObject;
using System.Xml;

namespace DataAccessLayer
{
    /// <summary>
    /// Define SubCategoryDao class. This class inherit from  SubCategoryDaoBase class.
    /// If you want add new function, you must define here.
    /// </summary>
    public class SubCategoryDao : SubCategoryDaoBase
    {
        private static ILog logger = null;
        #region Constructor
        /// <summary>
        /// Define Constructor.
        /// </summary>
        public SubCategoryDao() { }
        static SubCategoryDao()
        {
            // Init logger.
            logger = LoggingUtility.GetLogger(typeof(SubCategoryDao).FullName);
        }
        public static List<SubCategoryResult> GetAllByCategory(string categoryId)
        {
            String methodName = "GetAllByCategory";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }

            try
            {
                List<SubCategoryResult> listSubCategory = new List<SubCategoryResult>();
                DbCommand dbCommand = ManagerDao.GetStoredProcCommand("spGetAllSubCategoryByCateToControl");
                ManagerDao.AddInParameter(dbCommand, "CategoryId", DbType.String, categoryId);

                using (IDataReader reader = ManagerDao.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        listSubCategory.Add(GetSubCategoryResultByDataReader(reader));
                    }
                    reader.Close();
                }

                return listSubCategory;
            }
            catch (Exception ex)
            {
                //write error message to log file.
                logger.Error(methodName, ex);
                return null;
            }
        }
        public static List<SubCategoryResult> GetDonViTinhByLoaiHangHoa(int maLoaiHangHoa)
        {
            String methodName = "GetDonViTinhByLoaiHangHoa";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }

            try
            {
                List<SubCategoryResult> listSubCategory = new List<SubCategoryResult>();
                DbCommand cmd = ManagerDao.GetStoredProcCommand("spGetDonViTinhByLoaiHangHoa");

                //Add value MaLoaiHangHoa  paramater.
                ManagerDao.AddInParameter(cmd, "MaLoaiHangHoa", DbType.Int32,maLoaiHangHoa);
              
                using (IDataReader reader = ManagerDao.ExecuteReader(cmd))
                {
                    while (reader.Read())
                    {
                        listSubCategory.Add(GetSubCategoryResultByDataReader(reader));                       
                    }
                    reader.Close();
                }
                return listSubCategory;
            }
            catch (Exception ex)
            {
                //write error message to log file.
                logger.Error(methodName, ex);
                return null;
            }
        }
        public static List<SubCategoryResult> GetDonViTinhByMatHang(string maMatHang)
        {
            String methodName = "GetDonViTinhByMatHang";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }

            try
            {
                List<SubCategoryResult> listSubCategory = new List<SubCategoryResult>();
                DbCommand cmd = ManagerDao.GetStoredProcCommand("spGetDonViTinhByMatHang");

                //Add value MaLoaiHangHoa  paramater.
                ManagerDao.AddInParameter(cmd, "MaMatHang", DbType.String, maMatHang);

                using (IDataReader reader = ManagerDao.ExecuteReader(cmd))
                {
                    while (reader.Read())
                    {
                        listSubCategory.Add(GetSubCategoryResultByDataReader(reader));
                    }
                    reader.Close();
                }
                return listSubCategory;
            }
            catch (Exception ex)
            {
                //write error message to log file.
                logger.Error(methodName, ex);
                return null;
            }
        }
    }
}
        #endregion